import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

import Login from '@/views/login/index.vue'
import Question from '@/views/question/index.vue'
import Video from '@/views/video/index.vue'
import User from '@/views/user/index.vue'
import Profile from '@/views/user/profile.vue'
import Chat from '@/views/user/chat.vue'
import Article from '@/views/article/index.vue'
import Home from '@/views/home/index.vue'
import Tabbar from '@/components/app-tabbar.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/question',
    components: {
      default: Question,
      tabbar: Tabbar
    }
  },
  {
    path: '/video',
    components: {
      default: Video,
      tabbar: Tabbar
    }
  },
  {
    path: '/user',
    components: {
      default: User,
      tabbar: Tabbar
    }
  },
  {
    path: '/profile',
    component: Profile
  },
  {
    path: '/chat',
    component: Chat
  },
  {
    path: '/article',
    components: {
      default: Article,
      tabbar: Tabbar
    }
  },
  {
    path: '/',
    components: {
      default: Home,
      tabbar: Tabbar
    }
  }
]

const router = new VueRouter({
  routes
})
// 前置守卫
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  if (store.state.user.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
